<?php
//合同订单
class SalesorderController extends Controller
{

    public function actionIndex()
    {
        $this->render('index');
    }

    public function actionGetdata(){

        $page = $_REQUEST['page'];
        $rows = $_REQUEST['rows'];
        $start = ($page-1)*$rows;
        $total = $this->connection->createCommand("select count(1) from crm_salesorder where deleted = 0 ")->queryScalar();
        $result = $this->connection->createCommand("select c.*,u.realname,cc.customer_id,cc.customer_name,c_co.contacter_name from crm_salesorder c left join 
crm_user u on c.customer_id = u.user_id  left JOIN crm_customer cc on c.customer_id = cc.customer_id LEFT JOIN crm_contacter 
c_co on c_co.contacter_id = c.contacter_id where c.deleted = 0  limit {$rows} offset {$start}")->queryAll();

        $data = Array();
        $data['total'] = $total;
        $data['rows'] = $result;

        echo json_encode($data);
    }

    public function actionShowedit(){

        $salesorder_id = $_REQUEST['salesorder_id'];

        $salesorder = null;
        if($salesorder_id==0){
            //新增
        }else{
            //编辑
            $salesorder = $this->connection->createCommand("select c.*,u.realname,cc.customer_id,cc.customer_name,c_co.contacter_name from crm_salesorder c left join 
crm_user u on c.customer_id = u.user_id  left JOIN crm_customer cc on c.customer_id = cc.customer_id LEFT JOIN crm_contacter c_co on c_co.contacter_id = c.contacter_id
 where c.salesorder_id = :salesorder_id")->bindParam(':salesorder_id',$salesorder_id,PDO::PARAM_INT)->queryRow();
        }

        $this->render('showedit',['salesorder'=>$salesorder]);
    }

    public function actionSavedata(){

        $salesorder_id = $_REQUEST['salesorder_id'];
        $salesorder_number = $_REQUEST['salesorder_number'];
        $customer_id = $_REQUEST['customer_id'];
        $stage = $_REQUEST['stage'];
        $contacter_id = $_REQUEST['contacter_id'];
        $signdate = $_REQUEST['signdate'];
        $gather_status = $_REQUEST['gather_status'];

        if($salesorder_id!=null){
            //编辑
            $this->connection->createCommand("update crm_salesorder set salesorder_number = :salesorder_number,customer_id = :customer_id
,stage = :stage,contacter_id = :contacter_id,signdate = :signdate,gather_status = :gather_status
where salesorder_id = :salesorder_id")
                ->bindParam(':salesorder_id',$salesorder_id,PDO::PARAM_INT)
                ->bindParam(':salesorder_number',$salesorder_number,PDO::PARAM_STR)
                ->bindParam(':customer_id',$customer_id,PDO::PARAM_INT)
                ->bindParam(':stage',$stage,PDO::PARAM_STR)
                ->bindParam(':contacter_id',$contacter_id,PDO::PARAM_STR)
                ->bindParam(':signdate',$signdate,PDO::PARAM_STR)
                ->bindParam(':gather_status',$gather_status,PDO::PARAM_STR)
                ->execute();
        }else{
            //添加
            $this->connection->createCommand("insert into crm_salesorder (salesorder_number,customer_id,stage,contacter_id,signdate,gather_status) values 
(:salesorder_number,:customer_id,:stage,:contacter_id,:signdate,:gather_status) ")
                ->bindParam(':salesorder_number',$salesorder_number,PDO::PARAM_STR)
                ->bindParam(':customer_id',$customer_id,PDO::PARAM_INT)
                ->bindParam(':stage',$stage,PDO::PARAM_STR)
                ->bindParam(':contacter_id',$contacter_id,PDO::PARAM_STR)
                ->bindParam(':signdate',$signdate,PDO::PARAM_STR)
                ->bindParam(':gather_status',$gather_status,PDO::PARAM_STR)
                ->execute();
        }
    }


    public function actionRemove(){
        $salesorder_id = $_REQUEST['salesorder_id'];
        $this->connection->createCommand("update crm_salesorder set deleted = 1 
where salesorder_id = :salesorder_id")
            ->bindParam(':salesorder_id',$salesorder_id,PDO::PARAM_INT)
            ->execute();
    }

}